package com.huawei.basic.controller;

import com.huawei.basic.utils.AjaxResult;
import com.huawei.basic.utils.FastdfsUtil;
import net.sf.jsqlparser.schema.MultiPartName;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

/** 
* @Description: 文件操作接口
* @Author: chen
* @Date: 2022.5.5
*/
@RestController
@RequestMapping("/fastDfs")
public class FastDfsController {
    
    // 文件上传接口
    @PostMapping
    public AjaxResult upload(@RequestPart(value = "file",required = true) MultipartFile file){
        try {
            String filename = file.getOriginalFilename();
            String extName = filename.substring(filename.lastIndexOf(".") + 1);
            String resultPath = FastdfsUtil.upload(file.getBytes(), extName);
            return AjaxResult.me().setResultObj(resultPath);
        } catch (IOException e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("系统繁忙，请稍后重试！");
        }
    }
    
    // 文件删除接口
    @DeleteMapping
    public AjaxResult delete(@RequestParam(value = "path",required = false) String path){
        try {
            String pathTmp = path.substring(1); // goup1/xxxxx/yyyy
            String groupName =  pathTmp.substring(0, pathTmp.indexOf("/")); //goup1
            String remotePath = pathTmp.substring(pathTmp.indexOf("/")+1);// /xxxxx/yyyy
            System.out.println(groupName);
            System.out.println(remotePath);
            FastdfsUtil.delete(groupName,remotePath);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("系统繁忙，请稍后重试！");
        }
    }

}